/* eslint-disable react/jsx-key */
import React, { useState, useRef, useMemo } from 'react';
import ReactDOM from 'react-dom';
import { Card, Switch, Button, Modal, message, Image } from 'antd';
import AutoTable from '@/components/AutoTable';
import InitForm from '@/components/InitForm';
import { company_bc, customer, store } from '@/services/customer';
import { company } from '@/services/business';
import { connect } from 'umi';
import { PlusOutlined } from '@ant-design/icons';
import moment from 'moment';
import CompanyName from '@/components/CompanyName';


// type 类型有 table treeselect upload inputnumber datepicker daterange radio select textarea autoinput editor password input

function Bcnotice(props) {
  const [vs, cvs] = useState(false), //表单显/隐
    [fields, cf] = useState({
      company_id: {
        value: null,
        type: 'select',
        title: '代账公司',
        name: ['company_id'],
        required: true,
        options: {
          database: company,
          params: {
            is_all: 1,
            type: -1,
            account_user_id: props.user.currentUser.id,
            status: 'completed',
            service_type: 'account',
          },
        },
        col: { span: 24 },
      },
      year: {
        value: null,
        type: 'datepicker',
        title: '申报年份',
        name: ['year'],
        format: 'YYYY',
        required: true,
        disabledDate: (current) => {
          return current && current > moment().endOf('day');
        },
        picker: 'year',
        col: { span: 24 },
      },
      images: {
        value: null,
        type: 'upload',
        title: '申报截图',
        name: ['images'],
        required: true,
        listType: 'img',
        multiple: false,
        limit: 20,
      },
    }),
    [key, setkey] = useState('1'),
    [iftype, ciftype] = useState({});

  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const columns = useMemo(() => {
    if (key == '1') {
      return [
        {
          title: '公司名称',
          width: 260,
          dataIndex: 'name',
          key: 'name',
          render: (_, row) => (
            <CompanyName
              row={{ ...row, name: row?.company_name??row.name, id: row?.company_id??row.id }}
              reload={reload}
            />
          ),
        },
        {
          title: '客户名称',
          dataIndex: 'customer_name',
          key: 'customer_name',
          search: false,
        },
        {
          title: '记账会计',
          dataIndex: 'account_user_name',
          key: 'account_user_name',
          search: false,
        },
        {
          title: '成立日期',
          dataIndex: 'establish_date',
          key: 'establish_year',
          valueType: 'dateYear',
          render: (text, row) => {
            return row?.establish_date ? moment(row.establish_date).format('YYYY-MM-DD') : '-';
          },
        },
        {
          title: '申报截图',
          dataIndex: 'images',
          key: 'images',
          search: false,
          render: (_, row) => {
            let images = row?.tax_info?.images ?? [];
            if (!images) {
              return <b>暂未申报</b>;
            } else {
              return images.map((it) => {
                return <Image width={32} height={32} src={it.origin_url} style={{ padding: 2 }} />;
              });
            }
          },
        },
        {
          title: '操作',
          valueType: 'option',
          width: 160,
          render: (text, row, _, action) => {
            return (
              <Button
                type="link"
                disabled={row.account_user_id != props.user.currentUser.id}
                onClick={() => {
                  if (row?.tax_info?.id) {
                    message.warn(row.name + '本年已申报！');
                    return;
                  }
                  cvs(true);
                  cf((fields) => {
                    for (let i in fields) {
                      fields[i].value = null;
                      if (i == 'company_id') {
                        fields[i].value = row.id;
                        fields[i].disabled = true;
                      }
                      if (i == 'year') {
                        fields[i].value = moment();
                        fields[i].disabled = true;
                      }
                    }
                    return { ...fields };
                  });
                  ciftype({
                    val: 'add',
                    title: '添加工商年报',
                  });
                }}
              >
                添加工商年报
              </Button>
            );
          },
        },
      ];
    } else {
      return [
        {
          title: '公司名称',
          width: 260,
          dataIndex: 'company_name',
          key: 'company_name',
          render: (_, row) => (
            <CompanyName
              row={{ ...row, name: row?.company_name ?? row?.name, id: row?.company_id ?? row?.id }}
              reload={reload}
            />
          ),
        },
        {
          title: '客户名称',
          dataIndex: 'customer_name',
          key: 'customer_name',
          search: false,
        },
        {
          title: '申报年份',
          dataIndex: 'year',
          key: 'year',
          search: false,
        },
        {
          title: '成立日期',
          dataIndex: 'establish_date',
          key: 'establish_year',
          valueType: 'dateYear',
          render: (text, row) => {
            return row?.establish_date ? moment(row.establish_date).format('YYYY-MM-DD') : '-';
          },
        },
        {
          title: '申报截图',
          dataIndex: 'images',
          key: 'images',
          search: false,
          render: (_, row) => {
            if (!row.images) {
              return (
                <Image
                  width={32}
                  height={32}
                  style={{ padding: 2 }}
                  src={require('@/assets/default.png')}
                />
              );
            } else {
              return row.images.map((it) => {
                return <Image width={32} height={32} src={it.origin_url} style={{ padding: 2 }} />;
              });
            }
          },
        },
      ];
    }
  }, [key]);

  let extrarender = [
    <div
      className="action"
      title="新增"
      onClick={() => {
        cvs(true);
        cf((fields) => {
          for (let i in fields) {
            fields[i].value = null;
            fields[i].disabled = false;
          }
          return { ...fields };
        });
        ciftype({
          val: 'add',
          title: '添加工商年报',
        });
      }}
    >
      <PlusOutlined />
    </div>,
  ];

  let saveData = (values) => {
    let { dispatch } = props;
    if (iftype.val == 'add') {
      dispatch({
        type: 'customer/addcompany_bc',
        payload: values,
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    }
  };

  return (
    <div className="pgcontainer">
      <AutoTable
        tabList={[
          {
            key: '1',
            tab: '本年报送',
          },
          {
            key: '2',
            tab: '全部报送',
          },
        ]}
        onTabChange={(val) => {
          setkey(val);
        }}
        pagetitle={props.route.name} //页面标题
        pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        path={
          key == '1' ? '/wbn-api/company_bc/current_company_bc' : '/wbn-api/company_bc/bc_record'
        }
        extraparams={{ account_user_id: props.user.currentUser.id }}
      />
      <Modal
        maskClosable={false}
        title={iftype.title}
        open={vs}
        onCancel={() => cvs(false)}
        footer={false}
        destroyOnClose={true}
      >
        <InitForm
          fields={fields}
          submitData={(values) => {
            saveData(values);
          }}
          onChange={(changedValues, allValues) => {
            //联动操作
          }}
          submitting={props.loading.effects['customer/company_bc'] || !vs}
        />
      </Modal>
    </div>
  );
}

export default connect(({ customer, user, loading }) => ({
  customer,
  user,
  loading,
}))(Bcnotice);
